﻿using Autofac;
using log4net.Config;
using DJY.Code;
using DJY.Data;
using System;
using System.Data.Entity;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Routing;

namespace DJY.Web
{
    public class MvcApplication : System.Web.HttpApplication
    {
        /// <summary>
        /// 启动应用程序
        /// </summary>
        protected void Application_Start()
        {
            AutoMapperConfig.Init();
            Logs.Config();
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            OperatorProvider.RegisteService(c =>
            {
                c.RegisterInstance(CacheFactory.Create()).As<ICache>().SingleInstance();
                //数据库访问
                c.RegisterType<DemoContext>().As<DbContext>().InstancePerLifetimeScope();
                Type baseType = typeof(IDependency);
                Assembly[] asms = { Assembly.Load("DJY.Application"), Assembly.Load("DJY.Repository") };
                var types = c.RegisterAssemblyTypes(asms);
                types.Where(t => 
                t.IsClass
                && !t.IsAbstract
                && baseType.IsAssignableFrom(t)).AsImplementedInterfaces().SingleInstance();
            });
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}